//获取所有图书借阅信息
window.onload = function () {
    const token = sessionStorage.getItem("token")
    axios({
        url: "http://localhost:8080/admin/bookLend/querybook",
        method: "post",
        headers: { token }
    })
        .then(res => {
            bookRender(res.data.data)
        })
}
//封装渲染
function bookRender(data) {
    const tbody = document.querySelector('tbody')
    tbody.innerHTML = ""
    data.forEach((item, index) => {
        let str = `
        <tr>
            <td>${index + 1}</td>
            <td>${item.name}</td>
            <td>${item.book_lend}</td>
            <td>${item.lend_number}</td>
            <td>
                <button class="btn btn-danger btn-sm" data-id=${item.book_id}>删除</button>
                <button class="btn btn-warning btn-sm" data-id=${item.book_id} data-bs-toggle="modal" data-bs-target="#mmyModal">修改</button>
            </td>
        </tr>
        `
        tbody.innerHTML += str;
    });
}

const tbody = document.querySelector("tbody")
//删除操作
tbody.addEventListener("click", (e) => {
    const token = sessionStorage.getItem("token")
    if (e.target.className.includes("danger")) {
        const res = confirm("您确定删除嘛");
        if (res) {
            const book_id = e.target.dataset.id
            axios({
                url: "http://localhost:8080/admin/bookLend/delBook",
                method: "post",
                headers: { token },
                data: {
                    book_id
                }
            })
                .then(res => {
                    location.reload();
                })
        }
    }

    //根据id获取信息
    if (e.target.className.includes("warning")) {
        const book_id = e.target.dataset.id
        axios({
            url: "http://localhost:8080/admin/bookLend/findBook",
            method: "post",
            headers: { token },
            data: { book_id }
        })
            .then(res => {
                console.log(res.data.data);
                document.getElementById('hid').value = res.data.data.book_id;
                document.getElementById('hname').value = res.data.data.name;
                document.getElementById("hlend").value = res.data.data.book_lend;
                document.getElementById("hnumber").value = res.data.data.lend_number;
            })
    }

})
//添加操作操作
function addBook() {
    const name = document.getElementById("name").value.trim();
    const lend = document.getElementById("lend").value.trim();
    const number = document.getElementById("number").value.trim();
    const token = sessionStorage.getItem("token")
    axios({
        url: "http://localhost:8080/admin/bookLend/addBook",
        method: "post",
        headers: { token },
        data: {
            name, lend, number
        }
    })
        .then(res => {
            location.reload();
        })
}
// 修改操作
function updateBook() {
    const book_id = document.getElementById('hid').value.trim()
    console.log(book_id);
    const name = document.getElementById('hname').value.trim()
    const book_lend = document.getElementById("hlend").value.trim()
    const lend_number = document.getElementById("hnumber").value.trim()
    const token = sessionStorage.getItem("token")
    axios({
        url: "http://localhost:8080/admin/bookLend/updateBook",
        method: "post",
        headers: { token },
        data: { book_id, name, book_lend, lend_number }
    })
        .then(res => {
            location.reload();
        })
}